package com.ws.common.base;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ws.common.constant.BaseConstants;
import com.ws.common.result.entity.ResultEntity;
import com.ws.common.utils.SessionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Slf4j
public class BaseController<T extends BaseEntity> {

    @Autowired
    protected IService<T> iService;
    //protected BaseMapper<T> baseMapper;

    @RequestMapping("/listAll")
    public ResultEntity listAll(){
        try {
            Thread.sleep(800); //下版本优化速度
        }catch (Exception e){

        }

        return ResultEntity.succes(iService.list());
    }
    @RequestMapping("/read")
    public ResultEntity read(String id){
        return ResultEntity.succes(iService.getById(id));
    }

    @RequestMapping("/save")
    public ResultEntity save(T t){
        BaseUtils.setDefault(t);
        return ResultEntity.succes(iService.saveOrUpdate(t));
    }

    @RequestMapping("/delete")
    public ResultEntity delete(T t){
        t.setIsDeleted(BaseConstants.YES);
        return ResultEntity.succes(iService.updateById(t));
    }

    protected <P extends BaseEntity> P getLoginUser(){
        return SessionUtils.get("user");
    }
}
